package com.focus.xyz.other.pages.tpytb.baby;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.PinyinUtils;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;

/**
 * Created by chenmeng on 2016/5/16.
 */
public class TBPage {
    private WebDriver driver;
    private Action action;

    @FindBy(id = "_policyHolderBtn")
    private List<WebElement> isSelf;

    @FindBy(xpath = "//table[@id='datatable3']/tbody/tr/td[@class='CPanel']")
    private WebElement insuredTable;


    @FindBy(xpath = "//td[@id='base_applyName']/input[@name='applyName']")
    private WebElement tbrInput;
    @FindBy(xpath = "//td[@id='base_applyCerc']/input[@name='applyCerc']")
    private WebElement tbrCardNo;
    @FindBy(xpath = "//td[@id='base_customer.email']/input[@name='customer.email']")
    private WebElement email;
    @FindBy(name = "beginDate")
    private WebElement beginDate;

    @FindBy(xpath = "//table[@id='datatable3']/tbody")
    private WebElement insuredTab;

    @FindBy(id = "submitButton")
    private WebElement submitBtn;
    @FindBy(name = "factor_M9000")
    private WebElement schoolName;
    @FindBy(id = "sure_button")
    private WebElement confirmBtn;
    @FindBy(id = "printButton")
    private WebElement printBtn;

    @FindBy(id = "ajaxInfo")
    private WebElement ajaxInfo;


    public TBPage(WebDriver driver, Action action){
        this.driver = driver;
        this.action = action;
        PageFactory.initElements(driver,this);
    }

    public List<WebElement> getInsuredList() throws Exception{
        return insuredTab.findElements(By.tagName(Constants.TAG_TR));
    }


    public void fillInsuredInfo(String insuredName,String insuredCardId) throws Exception{
        String ninsuredName = StringUtils.replaceChars(insuredName," "," ");
        String ninsuredCardId = StringUtils.replaceChars(insuredCardId," "," ");
        List<WebElement> list = getInsuredList();
        if(getInsuredList().size()==2){
            WebElement row = list.get(1);
            List<WebElement> cols = row.findElements(By.tagName(Constants.TAG_TD));
            if(cols!=null&&cols.size()>0){
                action.click(cols.get(0).findElement(By.xpath("input[1]")));
                action.inputText(cols.get(2).findElement(By.xpath("input")),ninsuredName.trim());
                if(insuredCardId.length()<18){
                    action.select(cols.get(3).findElement(By.xpath("select")),"其他");
                }
                action.inputText(cols.get(4).findElement(By.xpath("input")),ninsuredCardId.trim());
                String pinyin = PinyinUtils.getPingYin(ninsuredName.trim());
                action.inputText(cols.get(5).findElement(By.xpath("input")),pinyin);
            }
        }
    }

    public void fillBaseInfo(String tbr,String cardNo,String email) throws Exception{
        String newTbr = StringUtils.replaceChars(tbr," "," ");
        String newcardNo = StringUtils.replaceChars(cardNo," "," ");
        String newemail = StringUtils.replaceChars(email," "," ");
        action.inputText(tbrInput,newTbr.trim());
        action.inputText(tbrCardNo,newcardNo.trim());
        action.inputText(this.email,newemail.trim());
        /*if(StringUtils.isNotBlank(beginDate)){
//            action.inputText(this.beginDate,beginDate);
//            System.out.println(this.beginDate.getAttribute(Constants.ATTR_VALUE));
            String newDate = StringUtils.replaceChars(beginDate,"/","-");
            action.setAttribute(this.beginDate,Constants.ATTR_VALUE,newDate);
        }*/
    }

    public void clickNotSelf() throws Exception{
        action.click(isSelf.get(1));
    }

    public void submit() throws Exception{
        action.click(submitBtn);
    }

    public void inputSchoolName(String ss) throws Exception{
        action.inputText(schoolName,ss);
    }

    public void confirm() throws Exception{
        action.click(confirmBtn);
    }

    public void print() throws Exception{
        action.click(printBtn);
    }

    public String getAlertContent() throws Exception{
        return driver.switchTo().alert().getText();
    }

    public void  waitAjaxInfo() throws Exception{
        WebDriverWait wait = new WebDriverWait(driver,10000l);
        wait.until(new ExpectedCondition<Boolean>() {

            public Boolean apply(WebDriver driver) {
                return !driver.findElement(By.id("ajaxInfo")).isDisplayed();
            }
        });
    }




}
